$(function() {
	mui.init()
	var code = sessionStorage.getItem("code")
	var name = sessionStorage.getItem("name")
	if(code == "03") {
		$(".lj").css("display", "none")
	} else if(code=='11'){
		$(".lj").css("display", "none")
		$(".xykhd").css("display","block")
	}
	$("title").text(name)
	//根据类型获取奖品
	$.post("selectPrizeByCode", { //根据类型获取奖品
		"code": code
	}, function(data) {
		if(data.code == 0) {
			var html = '';
			var rel = data.data
			/*	alert(JSON.stringify(rel))*/
			for(var i in rel) {
				html += '<div class="mui-input-row"><label>' + rel[i].奖品名称 + '(' + rel[i].备注 + '分)</label>';
				html += '<div class="mui-numbox"><button class="mui-btn mui-btn-numbox-minus"  type="button">-</button>';
				html += '<input class="mui-input-numbox" type="number" code="' + rel[i].id_prikey + '" jf="' + rel[i].备注 + '">';
				html += '<button class="mui-btn mui-btn-numbox-plus" type="button">+</button></div></div>'
			}
			$(".jp").html(html)
			mui.init()
		}
	})
	$(".jp").delegate('.mui-btn-numbox-plus', 'click', function() {
		var num = $(this).siblings("input").val()
		if(num == "") {
			num = 0
		}
		num++
		$(this).siblings("input").val(num)
	})
	$(".jp").delegate('.mui-btn-numbox-minus', 'click', function() {
		var num = $(this).siblings("input").val()
		if(num == "" || num == 0) {
			mui.alert("奖品数量已经是0了")
			return
		}
		num--
		$(this).siblings("input").val(num)
	})
	//根据钉钉ID获取营销信息
	$.post("selectUserByDdid", function(data) {
		/*alert(JSON.stringify(data))*/
		if(data.code == 0) {
			var rel = data.data;
			/*$("#gyh").val(rel.ryid)
			$("#yxr").val(rel.name)
			$("#yxwd").val(rel.deptName)*/
			deptId = rel.deptId
			djID = rel.deptId
		} else {
			mui.alert(data.error)
		}
	})
	//g根据柜员号获取营销人信息
	$("#gyh").change(function() {
		var code = $(this).val()
		if(code != "") {
			$.post("selectUserByUserId", {
				userId: code
			}, function(data) {
				if(data.code == 0) {
					var rel = data.data;
					$("#gyh").val(rel.ryid)
					$("#yxr").val(rel.name)
					$("#yxwd").val(rel.deptName)
					deptId = rel.deptId
				} else {
					mui.alert(data.error)
				}
			})
		}
	})
	//扫描客户二维码
	$("#sm").click(function() {
		dd.biz.util.scan({
			type: 'all', // type 为 all、qrCode、barCode，默认是all。
			tips: "长治漳泽农商银行", //进入扫码页面显示的自定义文案
			onSuccess: function(data) {
				var userid = data.text;
				$.post("getUserById", {
					userID: userid
				}, function(data) {
					if(data.code == 0) {
						var rel = data.data;
						$("#name").val(rel.name)
						$("#tel").val(rel.tel)
						$("#sfzh").val(rel.idcard)
						$("#gyh").val(rel.marketingManager)
						$("#yxr").val(rel.marketingManagerName)
						$("#yxwd").val(rel.marketingManagerDeptName)
						deptID = rel.marketingManagerDept
						userId = rel.userID
					} else {
						mui.alert(data.msg)
					}
				})

			},
			onFail: function(err) {

			}
		})
	})
	$("#ckje").blur(function() {
		var money = $(this).val()
		var sf = $("#sf").val()
		if(money != "") {
			if(Number(money) < 1) {
				mui.alert("金额不得低于一万元")
				$(this).val("")
				return
			}
			if(Number(money) > 5000) {
				mui.alert("请确认存款金额有5000万元吗？")
				$(this).val("")
				return
			}
			$.post("moneyConvertIntegral", {
				money: money,
				type: sf
			}, function(data) {
				if(data.code == 0) {
					$("#ckjf").val(data.data)
				} else {
					mui.alert(data.msg)
				}

			})
		}
	})
	$("#sf").change(function() {
		var sf = $(this).val()
		var money = $("#ckje").val()
		if(money != "") {
			if(Number(money) < 1) {
				mui.alert("金额不得低于一万元")
				$(this).val("")
				return
			}
			if(Number(money) > 5000) {
				mui.alert("请确认存款金额有5000万元吗？")
				$(this).val("")
				return
			}
			$.post("moneyConvertIntegral", {
				money: money,
				type: sf
			}, function(data) {
				if(data.code == 0) {
					$("#ckjf").val(data.data)
				} else {
					mui.alert(data.msg)
				}
			})
		}
	})
	$("#tj").click(function() {
		var name = $("#name").val()
		var tel = $("#tel").val()
		var sfzh = $("#sfzh").val()
		if(name == "") {
			mui.alert("请输入客户姓名")
			return
		}
		if(tel == "" || tel.length != 11) {
			mui.alert("请输入正确的手机号")
			return
		}
		tel = Base64.encode(tel)
		if(sfzh == "") {
			mui.alert("请输入客户身份证号")
			return
		}
		var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)|(^\d{17}(\d|X|x)(\d{2})$)/;
		if(reg.test(sfzh) == false) {
			mui.alert("请输入正确的身份证号")
			return
		}
		sfzh = Base64.encode(sfzh)
		var money1 = $("#ckje").val()
		if(code=='02'&&money1==""){
			mui.alert("请输入存款金额")
			return
		}
		
		var ckjf = $("#ckjf").val()
		if(ckjf == "" && code== "02") {
			mui.alert("请输入存款积分")
			return
		}
		if(code=="03"||code=='11'){
			ckjf=0
		}
		var xyk=$("#xyk").val()
		if(code=='11'&&xyk==""){
			mui.alert("请输入信用卡账号")
			return
		}
		xyk = Base64.encode(xyk)
		/*var cjkh = $("#cjkh").val()
		if(cjkh == "" && code == "03") {
			mui.alert("请输入抽奖卡号")
			return
		}
		if(cjkh.length != 6 && code == "01") {
			mui.alert("请输入六位抽奖卡号")
			return
		}*/
		var arr = []
		var str = []
		$(".mui-input-numbox").each(function(i) {
			if($(this).val() == "" || $(this).val() == 0) {

			} else {
				var rel = $(this).val()
				for(var i=0;i<rel;i++) {
					arr.push($(this).attr("code"))
					str.push($(this).attr("jf"))
				}
			}

		});
		if(arr.length == 0) {
			mui.alert("请选择奖品")
			return
		}

		/*var gyh = $("#gyh").val()*/

		var prize = arr.join(",")
		var prizeIntegral = str.join(",")
		var send = {
			"name": name,
			"tel": tel,
			"prize": prize,
			"id_prikey": "0",
			"activityType": code,
			"IDCard": sfzh,
			"money1": money1,
			"lotteryNumber": ckjf,
			"number1":xyk,
		/*	"voucherno": cjkh,*/
			/*"number5": gyh,*/
			/*"address": deptID,*/
			"prizeIntegral": prizeIntegral,
			"userID": userId
		}

		mui.showLoading("正在加载..", "div")
		console.log(6)
		if(code == "03") {
			tjsj(send)
		} else {
			$.post("selectPrizeNumberByDept", {
				"prizeId": prize,
				"dept": djID
			}, function(data) {
				if(data.code == 0) {
					tjsj(send)
				} else {
					mui.alert(data.msg)
					mui.hideLoading();
				}
			})
		}

	})

	function tjsj(send) {
		$.post("insertActivityNew", send, function(data) {
			console.log(JSON.stringify(data))
			if(data.code == 0) {
				mui.hideLoading();
				mui.alert("提交成功", function() {
					window.location.reload()
				})
			} else {
				mui.alert(data.msg)
				mui.hideLoading();
			}
		})

	}
})
var deptID = ''
var djID = ''
var userId = ''